<?php

namespace App\Controllers\Lessions;

use App\Models\Tag\Tags;
use \App\Libs\Views\Json;

class GetCourseIndex extends \App\Libs\Base\BaseAction
{

    public function execute()
    {
        $indexTagType = [1,2,3,4,5];
        $indexTags = Tags::findAll([
            'conditions' => 'tag_type in ({type:array}) and isshow=1',
            'bind' => ['type' =>$indexTagType ]
        ]);
        $finalData = [];
        $indexTags->rewind();
        while($indexTags->valid()){
            $currentTag = $indexTags->current();
            switch ($currentTag->tag_type){
                //垂直课程
                case '3':
                    $tagArrayMode = $currentTag->toArray(['id','tag_name','tag_type']);
//                    $extraData = json_decode($currentTag->data,true);
//                    if(isset($extraData['type'])){
//                        $tagArrayMode['type'] = $extraData['type'];
//                    }
                    $finalData['ageCourse'][] = $tagArrayMode;
                    break;
                    //孩子课程;
                case '2':
                    $tagArrayMode = $currentTag->toArray(['id','tag_name','tag_logo','tag_type']);
                    $extraData = json_decode($currentTag->data,true);
                    isset($extraData['playNum']) ? $tagArrayMode['playNum'] = $extraData['playNum'] : $tagArrayMode['playNum'] = '无数据';
                    //列表页才有的数据;
                    //isset($extraData['listLogo']) ? $tagArrayMode['listLogo'] = $extraData['listLogo'] : $tagArrayMode['listLogo'] = '无数据';
                    //isset($extraData['listIntro']) ? $tagArrayMode['listIntro'] = $extraData['listIntro'] : $tagArrayMode['listIntro'] = '无数据';

                    $finalData['childCourse'][] = $tagArrayMode;
                    break;
                //推荐试听；列表、进入之后，是纯列表；
                case '4':
                    $finalData['recomCourse'][] = $currentTag->toArray(['id','tag_name','tag_logo','tag_type']);
                    break;
                    //家长课程;
                case '1':
                    $finalData['parentCourse'][] = $currentTag->toArray(['id','tag_name','tag_logo','tag_type']);
                    break;
                    //banner
                case '5':
                    $tagArrayMode = $currentTag->toArray(['id','tag_name','tag_logo','tag_type']);
                    $extraData = json_decode($currentTag->data,true);
                    isset($extraData['jumpType']) ? $tagArrayMode['jumpType'] = $extraData['jumpType'] : $tagArrayMode['jumpType'] = '无数据';
                    isset($extraData['jumpData']) ? $tagArrayMode['jumpData'] = $extraData['jumpData'] : $tagArrayMode['jumpData'] = '无数据';
                    $finalData['banner'][] = $tagArrayMode;
                    break;
            }
            $indexTags->next();
        }

        return \App\Libs\Views\Json::view(0, '成功', $finalData);
    }
}
